package org.systm.menu.service.condition;

import org.springframework.data.jpa.domain.Specification;
import org.systm.menu.entity.Menu;
import org.systm.menu.entity.MenuType;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import java.util.List;
import java.util.function.Function;

public class MenuConditionQuery {
    public static Function<List<String>, Specification<Menu>> menuPermission = (menuIds) -> (r, q, cb) -> {
        Expression<Object> pid = r.get("pid");
        Expression<Boolean> del = r.get("del").as(Boolean.class);
        Expression<MenuType> menuType = r.get("menuType").as(MenuType.class);
        return cb.and(cb.in(pid).value(menuIds), cb.equal(del, Boolean.FALSE), cb.equal(menuType, MenuType.PERM));
    };

    public final static Function<List<String>, Specification<Menu>> findPer = (menuIds) -> (r, q, cb) -> {
        Expression<Boolean> delEx = r.get("del").as(Boolean.class);
        Path<Object> pidPath = r.get("pid");
        Expression<String> mtEx = r.get("menuType").as(String.class);
        return cb.and(cb.equal(delEx, Boolean.FALSE), pidPath.in(menuIds), cb.equal(mtEx, MenuType.PERM.getType()));
    };
}
